<?php
/*
 * @Author: 天羽落星辰
 * @Date: 2020-03-28 15:06:25
 * @LastEditors: 天羽落星辰
 * @LastEditTime: 2020-09-09 03:03:54
 * @Description: file content
 */


//发送get/post请求
function http($url,$params,$method='GET',$timeout=10){
    // return $url.'?'.http_build_query($params);
    
    // if(!is_array($params) || empty($params)){
    //     $params = [];
    // }
    // dump($params);die;

    if(strtoupper($method)=='POST'){
        $opts['http'] = [
            'method' => 'POST', 
            'timeout' => $timeout,    //$timeout秒超时
            // 'header' => 'Content-type:application/x-www-form-urlencoded',
            'header' => 'Content-type:application/json',
            'content' => ($params)
        ];
    }else{
        $opts['http'] = [
            'method' => 'GET', 
            'timeout' => $timeout
        ];
        $url=$url.'?'.http_build_query($params);
    }
    // dump($opts);die;
    $ret = @file_get_contents($url, false, stream_context_create($opts));
    return $ret;
}
